package org.tankwar.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.tankwar.dao.UserMapper;

/**
 * 使用简单工厂模式
 * mapper工厂类，专门用于创建mapper
 */
public class MapperFactory {
    // 获取一个SqlSessionFactory
    private static final SqlSessionFactory sqlSessionFactory = DaoUtil.getSqlSessionFactory();
    private static final SqlSession session = sqlSessionFactory.openSession();

    // 获取一个UserMapper接口的实现对象
    public static UserMapper getUserMapper() {
        return session.getMapper(UserMapper.class);
    }

    /**
     * 提交修改，更新到数据库中
     */
    public static void commitChanges() {
        session.commit();
    }

    /**
     * 关闭会话
     */
    public static void closeSession() {
        session.close();
    }
}
